<template>
  <div class="user-cotainer">
    <van-nav-bar
      style="z-index: 999"
      left-arrow
      @click-left="$router.back()"
      fixed
      title="校园超市"
    ></van-nav-bar>
    <div class="user-cover">
      <div class="user-info">
        <img
          class="avatar"
          :src="$baseUrl + userInfo.avatarurl"
          :alt="userInfo.nickname"
        />
        <h3 class="nickname">{{ userInfo.nickname }}</h3>
      </div>

      <div class="order">
        <div class="order-status">
          <div>红包</div>
          <div>优惠券</div>
          <div>鲜豆</div>
          <div>白条</div>
        </div>
      </div>
    </div>

    <van-cell-group>
      <van-cell is-link to="/addr" title="我的地址" value="地址管理" />
      <van-cell title="我的评价" value="评价管理" />
      <van-cell title="联系客服" value="在线投诉" />
      <van-cell>
        <van-button type="primary" round block @click="logout"
          >安全退出</van-button
        >
      </van-cell>
    </van-cell-group>
  </div>
</template>

<script setup>
import useCartStore from "@/store/cartStore";
import { inject, onMounted, ref } from "vue";
import { useRouter } from "vue-router";
const cartStore = useCartStore();
const { cartListInit } = cartStore;
const userInfo = ref({});
const router = useRouter();
const axios = inject("axios");
const logout = () => {
  localStorage.removeItem("userLoginInfo");
  localStorage.removeItem("store");
  // this.$toast('退出成功');
  // 清空购物车列表
  cartListInit([]);
  router.push({ name: "home" });
};
// 检测token有效期
const checkToken = () => {
  axios.get("/checktoken").then(() => {});
};
onMounted(() => {
  // 从本地存储中获取用户信息
  userInfo.value = JSON.parse(localStorage.getItem("userLoginInfo") || "{}");
  checkToken();
});
</script>

<style lang="scss">
.user-cotainer {
  background-color: #f1f1f1;
  height: 100vh;
  margin-top: 50px;
  margin-bottom: 100px;

  .user-cover {
    box-sizing: border-box;
    position: relative;
    height: 200px;
    width: 100%;
    background: #4498f7;
    color: #fff;
    padding: 10px;
    margin-bottom: 120px;
    border-radius: 0 0 60px 60px;

    .user-info {
      position: absolute;
      top: 15px;
      left: 50%;
      transform: translate(-50%);
      z-index: 1;

      .avatar {
        width: 100px;
        height: 100px;
        border-radius: 50%;
      }

      .nickname {
        color: #333;
      }
    }

    .order {
      box-sizing: border-box;
      position: absolute;
      bottom: -80px;
      left: 50%;
      background: #fff;
      width: 88%;
      height: 200px;
      border-radius: 10px;
      transform: translateX(-50%);
      color: #333;
      padding: 10px;

      .order-status {
        display: flex;
        justify-content: center;
        margin-top: 120px;
        padding-top: 20px;
        border-top: 1px solid #eee;

        div {
          flex: 1;
          text-align: center;
        }
      }
    }
  }
}
</style>